#!/bin/env  python
# -*- coding: gbk -*- 
import sys
path = "/home/spider/atscript/lib/"
sys.path.append(path)

from atscript import *
from hadoop import *
from assert_func import *

conf = config("rankget.yaml")
sample = base(conf.get("CONF"), conf.get("SCHEDULE"), conf.get("JOBLIST"))
hadoop = hadoop(conf)

class Test_rg_master_getsiteprepare(at_case):
    def __init__(self, casename, base, jobname, input, output):
        at_case.__init__(self, casename, base, jobname, input, output)
        
    def set_input(self):
        pass

    def run(self):
        print self.run_cmd
        self.execute_cmd(self.run_cmd, True)

    def check_result(self):
        # 1. ¼ì²é±¾µØÄ¿Â¼ºÍHDFSÄ¿Â¼ÊÇ·ñ´´½¨³É¹¦
        result_flag = True 
        if not expect_exsits(conf.get("MODULE_HOME")+"bin", conf.get("MODULE_HOME")+"log") :
            result_falg = False
              
        if not( hadoop.test("/user/spider/rankget/") and hadoop.test("/user/spider/rankget/bin") and hadoop.test("/user/spider/rankget/input") ):
            result_flag = False

        # 2. ¼ì²éÊÇ·ñÕýÈ·ÉÏ´«rankget.zipÎÄ¼þ, ¼ì²éÊÇ·ñÕýÈ·ÉÏ´«select_name_onlyÎÄ¼þ
        if not( hadoop.test("/user/spider/rankget/bin/rankget.zip") and hadoop.test("/user/spider/rankget/input/select_name_only") ):
            result_flag = False

        # 3. ¼ì²é»ñÈ¡µÄselect_name_onlyÊÇ·ñÕýÈ·
        output = subprocess.Popen("hadoop  dfs -ls /user/spider/dlb-linkbase/data/select/record ",shell=True, stdout = subprocess.PIPE).communicate()[0]
        path = output.split("\n")[-2].split(" ")[-1]+"/level_all"
        # print path
        output = subprocess.Popen("hadoop  dfs -ls %s " %path ,shell=True, stdout = subprocess.PIPE).communicate()[0].split("\n")
        
        expect_out = self.casename + ".expect_out"
        real_out = self.casename + ".real_out"

        file_handle = open(expect_out, 'w')
        for i in range(len(output)):
            line = output[i].split(" ")[-1]
            if line.startswith("/user/spider/dlb"):
               file_handle.write(line+"\n")
        file_handle.close()
 
        self.execute_cmd("rm -f %s" %real_out)
        hadoop.get("/user/spider/rankget/input/select_name_only", real_out)
          
        try:
            assert_file_equal(expect_out, real_out)        
        except AssertionError, e:
            result_flag = False
        if result_flag:
            print >>sys.stdout, "case %s passed" % self.casename
        else :
            print >>sys.stdout, "case %s failed" % self.casename

if __name__ == "__main__":
     test = Test_rg_master_getsiteprepare('Test_rg_master_getsiteprepare', sample, 'rg_master_getsiteprepare', '/user/spider/rankget/input/', '/user/spider/rankget/data/cslog/')
     test.set_input()
     test.run()
     test.check_result()  
      
